package com.clkj.goldpartner.controller;

import com.clkj.goldpartner.pojo.Category;
import com.clkj.goldpartner.pojo.common.Result;
import com.clkj.goldpartner.pojo.common.StatusCode;
import com.clkj.goldpartner.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 杨阿旺
 * @date 2021-02-28 16:27
 * 商品分类
 */
@RestController
@RequestMapping(value = "/category")
@CrossOrigin
@Api(value = "/api", tags = {"商品分类"})
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @ApiOperation(value = "获取商品分列列表")
    @PostMapping("/getCategory")
    public Result<Category> getCategory(Integer shop_id, Integer parent_id) {
        try {
            List<Category> categories = categoryService.listCategories(shop_id, parent_id);
            return new Result<>(true, StatusCode.OK, "查询成功", categories);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result<>(false, StatusCode.ERROR, "查询失败");
        }
    }

    /**
     * 添加商品获取分类
     *
     * @param shop_id
     * @return
     */
    @PostMapping("/getCategories")
    public Result<Category> getCategories(Integer shop_id) {
        try {
            List<Category> categories = categoryService.getCategories(shop_id);
            return new Result<>(true, StatusCode.OK, "查询成功", categories);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result<>(false, StatusCode.ERROR, "查询失败");
        }
    }

    @PostMapping("/addCategory")
    public Result addCategory(@RequestBody Category category) {
        try {
            categoryService.addCategory(category);
            return new Result<>(true, StatusCode.OK, "查询成功", "success");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result<>(false, StatusCode.ERROR, "查询失败", "error");
        }
    }

}
